package br.com.syshealth.clinica.manager;

import java.io.IOException;

import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.com.syshealth.clinica.entity.Pessoa;
import br.com.syshealth.clinica.session.PessoaDAO;
import br.com.syshealth.clinica.util.Utilidades;


@ManagedBean(name="loginManager")
@SessionScoped
public class LoginManager {

	@EJB
	private PessoaDAO pessoaDAO;
	
	private boolean loginOk;

	private String userName;

	private String password;

	public boolean isLoginOk() {
		return loginOk;
	}

	public void setLoginOk(boolean loginOk) {
		this.loginOk = loginOk;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = Utilidades.md5(password);
	}
	
	public String verificarUsuario() throws IOException{
	 Pessoa pessoa = null;
	 
	 FacesContext context = FacesContext.getCurrentInstance();
	 
	 HttpServletResponse rp = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();  
	 
	 
	 if(userName!= ""  && password != "")
	 {		 		 
		 pessoa = pessoaDAO.validaLogin(userName, password);
		 
		 if(pessoa != null)
		  {
			 	 
			 context.getExternalContext().getSessionMap().put("usuario", pessoa); 
		
			 
			 
			 
			 if(pessoa.getTipo().equals("3")){
			 
			 rp.sendRedirect("indexMedico.xhtml");
			 return "indexMedico.xhtml";
			 
			 }else if(pessoa.getTipo().equals("2")){
				 
				 rp.sendRedirect("indexPaciente.xhtml");
				 return "indexPaciente.xhtml";
			 } 
			 
			 rp.sendRedirect("index.xhtml");
			  return "index";
			  
		  }else{ 
			       this.setPassword("");
			  	context.addMessage(null, new FacesMessage("Login ou senha incorreto!"));
			  return "login"; 
			  }
	 }else 
	 		{ 
		      context.addMessage(null, new FacesMessage("Todos os campos devem ser preenchidos!"));
		 	
		 	return "login"; }
	  
	  
	
	}
	
	public String logOut() throws IOException
	{
//		FacesContext fc = FacesContext.getCurrentInstance();
//		ExternalContext ec = fc.getExternalContext();
//		HttpSession session = (HttpSession)ec.getSession( false );
//		session.removeAttribute("usuario");
		 FacesContext context = FacesContext.getCurrentInstance();
		 HttpServletResponse rp = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();  
		 
		 context.getExternalContext().getSessionMap().remove("usuario");
		 
		 rp.sendRedirect("../../clinica-syshealth-web/faces/login.xhtml");
		return "login ";
		
	
	}
	
}
